﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Web.Configuration;

namespace WXYW
{
    public partial class TFSQSign : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillFormData();
            }
        }

        private void FillFormData()
        {
            long applicationID = Convert.ToInt64(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]);
            TFSQService service = new TFSQService();
            SpecialServiceForm form = service.GetSpecialServiceForm(applicationID);
            SQRLbl.Text = form.User.ContactPerson;
            CustomerContactLbl.Text = form.ApplicationForm.User.ContactPerson;
            CustomerLbl.Text = form.ApplicationForm.User.FullName;
            CustomerPhoneLbl.Text = form.ApplicationForm.User.UserMobilePhone;
            CustomerEmailLbl.Text = form.ApplicationForm.User.UserEmail;
            JXSLbl.Text = form.SellerName;
            JXSManagerLbl.Text = form.SellerManager;
            JXSPhoneLbl.Text = form.SellerPhone;
            ReasonLbl.Text = form.ApplyReason;
            statusLbl.Text = form.Status;
            applyDateLbl.Text = form.ApplicationDate.HasValue ? form.ApplicationDate.Value.ToString("yyyy-MM-dd") : "";
            defineDateLbl.Text = form.DefineDate.HasValue ? form.DefineDate.Value.ToString("yyyy-MM-dd") : "";
            fillDateLbl.Text = form.FillDate.HasValue ? form.FillDate.Value.ToString("yyyy-MM-dd") : "";

            if (form.TFSQContent.Count > 0)
            {
                fwnrLbl.Text = form.TFSQContent.First().FWContent;
                departmentLbl.Text = form.TFSQContent.First().Department;
                startDateLbl.Text = form.TFSQContent.First().StartDate.ToString("yyyy-MM-dd");
                endDateLbl.Text = form.TFSQContent.First().EndDate.ToString("yyyy-MM-dd");
                costLbl.Text = "￥" + form.TFSQContent.First().Cost.ToString() + "元";
            }


            double totalCost = 0;
            foreach (SpecialServiceProduct serviceProduct in form.SpecialServiceProduct)
            {
                if (!serviceProduct.ApplicationProductReference.IsLoaded)
                {
                    serviceProduct.ApplicationProductReference.Load();
                }

                if (!serviceProduct.ApplicationProduct.QuatationForm.IsLoaded)
                {
                    serviceProduct.ApplicationProduct.QuatationForm.Load();
                }

                foreach (QuatationForm quatationForm in serviceProduct.ApplicationProduct.QuatationForm)
                {
                    totalCost += quatationForm.Cost;
                }
            }
            costLbl.Text = totalCost.ToString() + "元";
            RMALbl.Text = form.ApplicationForm.RunningNumber;

            ApplicationProductService productService = new ApplicationProductService();
            applicationProductGridView.DataSource = productService.GetQuatationProducts(form.ApplicationForm.ApplicationID);
            applicationProductGridView.DataBind();

            csDateTimeLbl.Text = form.User1SignDate.HasValue ? form.User1SignDate.Value.ToString("yyyy-MM-dd") : "";
            csCommentLbl.Text = form.User1SignType + " " + form.User1Comment;
            zsDateTimeLbl.Text = form.User2SignDate.HasValue ? form.User2SignDate.Value.ToString("yyyy-MM-dd") : "";
            zsCommentLbl.Text = form.User2SignType + " " + form.User2Comment;
            csrLbl.Text = form.User1.ContactPerson;
            zsrLbl.Text = form.User2.ContactPerson;
        }

        protected void applicationProductGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void okLinkButton_Click(object sender, EventArgs e)
        {
            string status = Request.QueryString[Constants.QUERYSTR_STATUS];
            long applicationID = Convert.ToInt64(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]);
            TFSQService service = new TFSQService();
            SpecialServiceForm form = service.GetSpecialServiceForm(applicationID);
            switch (status)
            {
                case "3":
                    if (shtypeRadioList.SelectedIndex == 0)
                    {
                        form.Status = "4";

                        // 发送邮件给第二位审批人
                        EmailService.SendEmail(form.User2.UserEmail, WebConfigurationManager.AppSettings["TFSQSHNoticeSubject"],
                                string.Format(WebConfigurationManager.AppSettings["TFSQSHNoticeBody"], form.User2.ContactPerson, form.ApplicationCode));
                    }
                    else
                    {
                        form.Status = "-1";            
                        // 发送邮件给第二位审批人
                        EmailService.SendEmail(form.User.UserEmail, WebConfigurationManager.AppSettings["TFSQRejectSubject"],
                                string.Format(WebConfigurationManager.AppSettings["TFSQRejectBody"], form.User.ContactPerson, form.ApplicationCode));
                    }
                    form.User1SignDate = DateTime.Now;
                    form.User1SignType = shtypeRadioList.SelectedValue;
                    form.User1Comment = commentTxtBox.Text;                    
                    break;
                case "4":
                    if (shtypeRadioList.SelectedIndex == 0)
                    {
                        form.Status = "5";
                        // 发送邮件给第三位审批人
                        EmailService.SendEmail(form.User3.UserEmail, WebConfigurationManager.AppSettings["TFSQSHNoticeSubject"],
                                string.Format(WebConfigurationManager.AppSettings["TFSQSHNoticeBody"], form.User3.ContactPerson, form.ApplicationCode));
                    }
                    else
                    {
                        form.Status = "-1";
                        // 发送邮件给第二位审批人
                        EmailService.SendEmail(form.User.UserEmail, WebConfigurationManager.AppSettings["TFSQRejectSubject"],
                                string.Format(WebConfigurationManager.AppSettings["TFSQRejectBody"], form.User.ContactPerson, form.ApplicationCode));
                    }
                    form.User2SignDate = DateTime.Now;
                    form.User2SignType = shtypeRadioList.SelectedValue;
                    form.User2Comment = commentTxtBox.Text;
                    break;
                case "5":
                    if (shtypeRadioList.SelectedIndex == 0)
                    {
                        form.Status = "6";
                        // 发送邮件给第二位审批人
                        EmailService.SendEmail(form.User.UserEmail, WebConfigurationManager.AppSettings["TFSQCompleteSubject"],
                                string.Format(WebConfigurationManager.AppSettings["TFSQCompleteBody"], form.User.ContactPerson, form.ApplicationCode));
                    }
                    else
                    {
                        form.Status = "-1";
                        // 发送邮件给第二位审批人
                        EmailService.SendEmail(form.User.UserEmail, WebConfigurationManager.AppSettings["TFSQRejectSubject"],
                                string.Format(WebConfigurationManager.AppSettings["TFSQRejectBody"], form.User.ContactPerson, form.ApplicationCode));
                    }
                    form.User3SignDate = DateTime.Now;
                    form.User3SignType = shtypeRadioList.SelectedValue;
                    form.User3Comment = commentTxtBox.Text;
                    break;
            }
            service.UpdateForm(form);
            Response.Write(string.Format("<script language='JavaScript'> alert( '记录已成功保存！');location.href= 'TFSQSignList.aspx' </script>"));
        }
    }
}
